package com.du.linandroid.biz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.du.linandroid.callback.CallBack;
import com.du.linandroid.callback.ShareCallBack;
import com.du.linandroid.biz.ApiHolder;
import com.du.linandroid.biz.IService;
import com.du.linandroid.biz.QQService;
import com.du.linandroid.biz.WechatService;
import com.du.linandroid.biz.QQShare;
import com.du.linandroid.biz.WXShare;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.tencent.tauth.Tencent;

/**
 * Created by DuZiLin on 2018/3/29.
 */

public class Lin {
    private static Context mContext;
    private static IService service;
    private static WXShare mWXShare;
    private static QQShare mQQShare;

    public static void init(Activity context, String WX_APP_ID, String WX_SECRET, String QQ_APP_ID, String QQ_SECRET ,String WEIBO_APP_KEY) {
        mContext = context;
        wechatInit(context, WX_APP_ID, WX_SECRET);
        qqInit(context, QQ_APP_ID, QQ_SECRET);
        weiboInit(context , WEIBO_APP_KEY);
    }

    public static void wechatInit(Activity context, String WX_APP_ID, String WX_SECRET) {
        if (!(TextUtils.isEmpty(WX_APP_ID) || TextUtils.isEmpty(WX_SECRET))) {
            ApiHolder.WX_APP_ID = WX_APP_ID;
            ApiHolder.WX_SECRET = WX_SECRET;
            ApiHolder.wechatInit(context);
        }
    }

    public static void qqInit(Activity context, String QQ_APP_ID, String QQ_SECRET) {
        if (!(TextUtils.isEmpty(QQ_APP_ID) || TextUtils.isEmpty(QQ_SECRET))) {
            ApiHolder.QQ_APP_ID = QQ_APP_ID;
            ApiHolder.QQ_SECRET = QQ_SECRET;
            ApiHolder.qqInit(context);
        }
    }

    public static void weiboInit(Context context , String WEIBO_APP_KEY){
        if(!TextUtils.isEmpty(WEIBO_APP_KEY)){
            ApiHolder.WEIBO_APP_KEY = WEIBO_APP_KEY;
            WbSdk.install(context , new AuthInfo(context , WEIBO_APP_KEY , "http://www.sina.com" , "email,direct_messages_read,direct_messages_write,friendships_groups_read,friendships_groups_write,statuses_to_me_read,follow_app_official_microblog,invitation_write"));
        }
    }



    /**
     * 微信登录 需提前调用init或wechatInit
     *
     * @param loginCallBack 登录回调
     */
    public static void wechatLogin(CallBack loginCallBack) {
        service = new WechatService(mContext);
        service.login(loginCallBack);
    }

    /**
     * qq登录 需提前调用init或qqInit
     *
     * @param activity      当前Activity
     * @param loginCallBack 登录回调
     */
    public static void qqLogin(Activity activity, CallBack loginCallBack) {
        service = new QQService(activity);
        service.login(loginCallBack);
    }


    /**
     * 微信分享网页消息
     *
     * @param isTimeLine  是否分享到朋友圈，false为分享给好友
     * @param webPageUrl  网页的网址
     * @param title       标题
     * @param description 内容描述
     * @param bMap        网页分享中的左侧小图片
     */
    public static void wechatWebPageShare(boolean isTimeLine, String webPageUrl, String title, String description, Bitmap bMap, ShareCallBack callBack) {
        if (mWXShare == null) {
            mWXShare = new WXShare(mContext, ApiHolder.WX_APP_ID);
        }

        mWXShare.shareWebPage(isTimeLine, webPageUrl, title, description, bMap, callBack);
    }

    /**
     * 微信分享文本消息
     *
     * @param isTimeLine 是否分享到朋友圈，false为分享给好友
     * @param title      文本内容
     */
    public static void wechatTextShare(boolean isTimeLine, String title, ShareCallBack callBack) {
        if (mWXShare == null) {
            mWXShare = new WXShare(mContext, ApiHolder.WX_APP_ID);
        }
        mWXShare.shareTextMessage(isTimeLine, title, callBack);
    }

    /**
     * 微信分享图片消息
     *
     * @param isTimeLine 是否分享到朋友圈，false为分享给好友
     * @param bMap       图片
     */
    public static void wechatImageShare(boolean isTimeLine, Bitmap bMap, ShareCallBack callBack) {
        if (mWXShare == null) {
            mWXShare = new WXShare(mContext, ApiHolder.WX_APP_ID);
        }
        mWXShare.shareImgMessage(isTimeLine, bMap, callBack);
    }

    public static void onActivityResult(int requestCode, int resultCode, Intent data) {
        //QQ login
        if (service instanceof QQService) {
            QQService qqService = (QQService) service;
            Tencent.onActivityResultData(requestCode, resultCode, data, qqService.getLoginLinstener());
        }
        //sina login
        if(ApiHolder.mSsoHandler!=null){
            ApiHolder.mSsoHandler.authorizeCallBack(requestCode,resultCode,data);
        }
    }

    /**
     * 分享给QQ好友
     *
     * @param title    标题
     * @param text     内容
     * @param url      点击后跳转的url
     * @param imageUrl 图片url
     * @throws Exception 使用前必须调用Lin.init()
     */
    public static void qqShare(String title, String text, String url, String imageUrl, ShareCallBack callBack) {
        if (mQQShare == null) {
            mQQShare = new QQShare((Activity) mContext);
        }
        mQQShare.shareQQ(title, text, imageUrl, url, callBack);
    }

    /**
     * 分享到QQ空间
     *
     * @param title    标题
     * @param text     内容
     * @param url      点击后跳转的url
     * @param imageUrl 图片url
     * @throws Exception 使用前必须调用Lin.init()
     */
    public static void qqZoneShare(String title, String text, String url, String imageUrl, ShareCallBack callBack) {
        if (mQQShare == null) {
            mQQShare = new QQShare((Activity) mContext);
        }
        mQQShare.shareQQZone(title, text, url, imageUrl, callBack);
    }

}
